package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.CategoryMapper;
import com.baizhi.dto.PageDTO;
import com.baizhi.dto.PageDTOs;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryExample;
import com.baizhi.service.CategoryService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryMapper categoryMapper;
    @AddCache
    @Override
    public HashMap<String, Object> queryOnePage(PageDTO pageDTO) {
        HashMap<String, Object> map = new HashMap<>();
        //设置当前页
        map.put("page",pageDTO.getPage());
        //设置条件查询对象
        CategoryExample example = new CategoryExample();
        example.createCriteria().andLevelsEqualTo(1);
        //查询总条数
        int count = categoryMapper.selectCountByExample(example);
        //设置总条数
        map.put("total",count);
        //创建分页对象   起始条数  数据数
        RowBounds rowBounds = new RowBounds((pageDTO.getPage()-1)* pageDTO.getPageSize(),pageDTO.getPageSize());
        //分页查询数据
        List<Category> feedbacks = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        //设置数据
        map.put("rows",feedbacks);
        return map;

    }
    @DelCache
    @AddLog(value = "删除类别信息")
    @Override
    public HashMap<String,Object> delete(Category category) {
        HashMap<String,Object> map = new HashMap<>();

        if(category.getParentId()==null){
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(category.getId());
            int count = categoryMapper.selectCountByExample(categoryExample);
            if(count==0){
                categoryMapper.delete(category);
                 map.put("message","一级类别删除成功");
            }else {
                map.put("message","一级类别下有二级类别不能删除");
            }
        }else{
            categoryMapper.delete(category);
            map.put("message","二级类别删除成功");
        }
       /* try {
            categoryMapper.delete(category);
            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }*/

      return map;

    }
    @DelCache
    @AddLog(value = "添加类别信息")
    @Override
    public HashMap<String, Object> add(Category category) {
        HashMap<String,Object> map = new HashMap<>();
        if(category.getParentId()!=null){
            category.setLevels(2);
        }else{
            category.setLevels(1);
        }
        try {
            category.setId(UUID.randomUUID().toString());
            categoryMapper.insertSelective(category);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Category queryById(String id) {

        return categoryMapper.selectByPrimaryKey(id);
    }
    @DelCache
    @AddLog(value = "修改类别信息")
    @Override
    public HashMap<String, Object> update(Category category) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            categoryMapper.updateByPrimaryKeySelective(category);
            map.put("message","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败");
        }
        return map;
    }
    @AddCache
    @Override
    public HashMap<String, Object> queryTwoPage(PageDTOs pageDTOs) {
        HashMap<String, Object> map = new HashMap<>();
        //设置当前页
        map.put("page",pageDTOs.getPage());
        //设置条件查询对象
        CategoryExample example = new CategoryExample();
        example.createCriteria().andParentIdEqualTo(pageDTOs.getCategoryId());
        //查询总条数
        int count = categoryMapper.selectCountByExample(example);
        //设置总条数
        map.put("total",count);
        //创建分页对象   起始条数  数据数
        RowBounds rowBounds = new RowBounds((pageDTOs.getPage()-1)* pageDTOs.getPageSize(),pageDTOs.getPageSize());
        //分页查询数据
        List<Category> feedbacks = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        //设置数据
        map.put("rows",feedbacks);
        return map;
    }

    @Override
    public List<Category> queryByLevelsCategory(Integer levels) {
        CategoryExample example = new CategoryExample();
        example.createCriteria().andLevelsEqualTo(levels);
        List<Category> list = categoryMapper.selectByExample(example);
        return list;
    }
}
